package com.rose.datax.dataxadmin.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.util.Objects;

/**
 * 数据库连接
 */
@TableName("db_connection")
@Data
public class DbConnection extends BaseEntity{

    private static final long serialVersionUID = 872310221503229169L;
    private String name;

    @TableField("db_type")
    private String dbType;

    private String host;
    private int port;
    private String db;
    @TableField("use_ssl")
    private boolean useSSL;
    private String username;
    @JsonIgnore
    private String password;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DbConnection that = (DbConnection) o;
        return port == that.port &&
                Objects.equals(name, that.name) &&
                Objects.equals(dbType, that.dbType) &&
                Objects.equals(host, that.host) &&
                Objects.equals(db, that.db) &&
                Objects.equals(username, that.username) &&
                Objects.equals(password, that.password);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, dbType, host, port, db, username, password);
    }

    @Override
    public String toString() {
        return "DbConnectionRequest{" +
                "name='" + name + '\'' +
                ", dbType='" + dbType + '\'' +
                ", host='" + host + '\'' +
                ", port=" + port +
                ", db='" + db + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    @JsonIgnore
    public String getJdbcURL(){
        return String.format("jdbc:mysql://%s:%d/%s?useUnicode=true&characterEncoding=UTF-8&useSSL=%s",this.getHost(),
                this.getPort(), this.getDb(),this.useSSL);
    }
}
